<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="showDialog = true">添加角色</el-button>

      </div>
      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column prop="name" align="center" width="200" label="角色">
          <template v-slot="{row}">
            <input v-if="row.isEdit" v-model="row.isShowInfo.name" type="text" size="mini">
            <span v-else>{{ row.name }}</span>

          </template>
        </el-table-column>
        <el-table-column align="center" width="200" label="启用">
          <!-- 自定义列结构 -->
          <template v-slot="{ row }">
            <el-switch v-if="row.isEdit" v-model="row.isShowInfo.state" :active-value="1" :inactive-value="0" />
            <span v-else>  {{ row.state === 1 ? "已启用" : row.state === 0 ? "未启用" : "无" }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="description" align="center" label="描述">
          <template v-slot="{row}">
            <el-input v-if="row.isEdit" v-model="row.isShowInfo.description" type="textarea" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="{row}">
            <template v-if="row.isEdit">
              <el-button type="primary" @click="submit(row)">确定</el-button>
              <el-button @click="row.isEdit=false">取消</el-button>
            </template>
            <template v-else>
              <el-button size="mini" type="text" @click="assignPM">分配权限</el-button>
              <el-button size="mini" type="text" @click="edit(row)">编辑</el-button>
              <el-button size="mini" type="text" @click="del(row.id)">删除</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination :page-size="pageParams.pageSize" :current-page="pageParams.page" :total="pageParams.total" layout="prev, pager, next" @current-change="changePage" />
      </el-row>
    </div>
    <el-dialog width="500px" title="新增角色" :visible.sync="showDialog" @close="btnCancel">
      <!-- 表单内容 -->
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="roleForm.name" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item label="启用">
          <!-- 如果不需要校验 就不需要写 prop属性 -->
          <el-switch v-model="roleForm.state" :active-value="1" :inactive-value="0" size="mini" />
        </el-form-item>
        <el-form-item prop="description" label="角色描述">
          <el-input v-model="roleForm.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item label-width="150px">
          <el-button type="primary" @click="btnOK">确定</el-button>
          <el-button type="success" @close="btnCancel">取消</el-button>
        </el-form-item>

      </el-form></el-dialog>
    <el-dialog :visible.sync="showDialogPermission" title="分配权限">
      <el-tree :data="permissionData" :props="{label:'name'}" show-checkbox default-expand-all />
    </el-dialog>

  </div>

</template>
<script>
import { getRoleList, addRole, put, del } from '@/api/role'
import { getPermissionList } from '@/api/safe'
import { transformTreeData } from '@/utils/index'
export default {
  data() {
    return {
      list: [],
      pageParams: {
        page: 1,
        pageSize: 3,
        total: null
      },
      showDialog: false,
      roleForm: {
        name: '',
        description: '',
        state: 0 // 默认未1启用 关闭 0 打开1
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      showDialogPermission: false,
      permissionData: []
    }
  },
  async created() {
    this.getRole()
  },
  methods: {
    async getRole() {
      const { rows, total, pageSize, page } = await getRoleList(this.pageParams)
      this.list = rows
      this.pageParams.total = total
      this.pageParams.pageSize = pageSize
      this.pageParams.page = page

      console.log(rows, 35)
      this.list.forEach((item) => {
        this.$set(item, 'isEdit', false)
      })
      this.list.forEach((item) => {
        this.$set(item, 'isShowInfo', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
    },
    // 切换分页时 请求新的数据
    changePage(newPage) {
      this.pageParams.page = newPage // 赋值当前页码
      this.getRole()
    },
    btnOK() {
      this.$refs.roleForm.validate(async isOK => {
        if (isOK) {
          await addRole(this.roleForm)
          this.$message.success('新增角色成功')
          this.getRole()
          this.btnCancel()
        }
      })
    },
    btnCancel() {
      this.$refs.roleForm.resetFields() // 重置表单数据
      this.showDialog = false // 关闭弹层
    },
    edit(row) {
      console.log(row, 112)
      row.isEdit = true
      row.name = row.isShowInfo.name
      row.state = row.isShowInfo.state
      row.description = row.isShowInfo.description
    },
    async submit(row) {
      if (row.isShowInfo.name && row.isShowInfo.description) {
        await put({ ...row.isShowInfo, id: row.id })
        this.$message.success('修改成功')
        Object.assign(row, {
          ...row.isShowInfo,
          isEdit: false
        })
      } else {
        this.$message.warning('请输入角色和描述')
      }
    },
    async del(id) {
      await del(id)

      this.$message.success('删除成功')
      this.getRole()
    },
    async  assignPM() {
      this.showDialogPermission = true
      this.permissionData = transformTreeData(await getPermissionList(), 0)
      console.log(this.permissionData, '分配权限数据')
    }

  }
}
</script>

<style scoped>
.role-operate {
  padding: 10px;
}
</style>
